package org.cxn.tools.simple;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 时间日期操作
 * Created by chenxiangning on 2015/8/9.
 */
public class DateTool {
    /**
     * 获取当前时间 前几天 后几天
     *
     * @param currentTime yyyy-MM-dd HH:mm:ss
     * @param t
     * @return
     */
    public static String currentTime(String currentTime, int t) {

        SimpleDateFormat sdf = new SimpleDateFormat(currentTime);

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, t);
        String time = sdf.format(cal.getTime());

        return time;
    }

    /**
     * 计算两个时间的差距
     *
     * @param beginTime 开始时间
     * @param closeTime 结束时间
     * @return 两个时间差 单位天
     * @author cxn
     */
    public static int distanceTime(String beginTime, String closeTime) {
        long quot = 0;
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date beginDate = ft.parse(beginTime);
            Date closeDate = ft.parse(closeTime);
            quot = closeDate.getTime() - beginDate.getTime();
            quot = quot / 1000 / 60 / 60 / 24;
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return (int) quot;
    }


    /**
     * 两个日期相差的天数
     *
     * @param d1
     * @param d2
     * @return
     */
    public static long calendarDayPlus(Date d1, Date d2) {
        long milis = calendarMilisPlus(d1, d2);
        milis = milis / 1000 / 60 / 60 / 24;
        return milis;
    }

    /**
     * 两个日期相差的小时数
     *
     * @param d1
     * @param d2
     * @return
     */
    public static long calendarHourPlus(Date d1, Date d2) {
        long milis = calendarMilisPlus(d1, d2);
        milis = milis / 1000 / 60 / 60;
        return milis;
    }

    /**
     * 两个日期相差的分钟数
     *
     * @param d1
     * @param d2
     * @return
     */
    public static long calendarMinutePlus(Date d1, Date d2) {
        long milis = calendarMilisPlus(d1, d2);
        milis = milis / 1000 / 60;
        return milis;
    }

    /**
     * 两个日期相差的秒数
     *
     * @param d1
     * @param d2
     * @return
     */
    public static long calendarSecondPlus(Date d1, Date d2) {
        long milis = calendarMilisPlus(d1, d2);
        milis = milis / 1000;
        return milis;
    }

    /**
     * 两个日期相差的毫秒数
     *
     * @param d1
     * @param d2
     * @return
     */
    public static long calendarMilisPlus(Date d1, Date d2) {
        long x = 0;
        java.util.GregorianCalendar gc1 = new java.util.GregorianCalendar();
        gc1.setTime(d1);
        java.util.GregorianCalendar gc2 = new java.util.GregorianCalendar();
        gc2.setTime(d2);
        x = gc2.getTimeInMillis() - gc1.getTimeInMillis();
        gc1 = null;
        gc2 = null;
        return x;
    }

    /**
     * 两个日期相差的周数
     *
     * @param d1
     * @param d2
     * @return
     */
    public static long calendarWeekPlus(Date d1, Date d2) {
        long x = 0;
        java.util.GregorianCalendar gc1 = new java.util.GregorianCalendar();
        gc1.setTime(d1);
        java.util.GregorianCalendar gc2 = new java.util.GregorianCalendar();
        gc2.setTime(d2);
        x = gc2.get(Calendar.WEEK_OF_YEAR) - gc1.get(Calendar.WEEK_OF_YEAR);

        gc1 = null;
        gc2 = null;
        return x;
    }

    /**
     * 求某年下的第几个星期的日期 返回java.uilt.Date 类型日期 时间time为当前机器时间
     *
     * @param year 要获得的年
     * @param week 第几个星期
     * @param flag 是否是第一天还是最后一天,当为true时返回第一天,false则返回最后一天
     * @return java.uilt.Date 类型日期
     */

    public static Date getDayByWeek(int year, int week, boolean flag) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.WEEK_OF_YEAR, week);
        if (!flag)
            cal
                    .setTimeInMillis(cal.getTimeInMillis() + 6 * 24 * 60 * 60
                            * 1000);
        return cal.getTime();
    }

    /**
     * 在某一日期上加减天数然后得到新的日期
     *
     * @param date
     * @param day
     * @return
     */
    public static Date getDateByDateAddSubDay(Date date, int day) {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(date);
        gc.add(5, day);
        return gc.getTime();
    }

    /**
     * 返回指定年月的月的最后一天
     *
     * @param year
     * @param month
     * @return
     */
    public static Date getLastDayOfMonth(Integer year, Integer month) {
        Calendar calendar = Calendar.getInstance();
        if (year == null) {
            year = calendar.get(Calendar.YEAR);
        }
        if (month == null) {
            month = calendar.get(Calendar.MONTH);
        }
        calendar.set(year, month - 1, 1);
        calendar.roll(Calendar.DATE, -1);
        return calendar.getTime();
    }

    /**
     * 返回指定日期的月的最后一天
     *
     * @param date
     * @return
     */
    public static Date getLastDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                1);
        calendar.roll(Calendar.DATE, -1);
        return calendar.getTime();
    }

    /**
     * 返回指定年月的月的第一天
     *
     * @param year
     * @param month
     * @return
     */
    public static Date getFirstDayOfMonth(Integer year, Integer month) {
        Calendar calendar = Calendar.getInstance();
        if (year == null) {
            year = calendar.get(Calendar.YEAR);
        }
        if (month == null) {
            month = calendar.get(Calendar.MONTH);
        }
        calendar.set(year, month - 1, 1);
        return calendar.getTime();
    }

    /**
     * 获取某年的第几周的开始日期
     *
     * @param year
     * @param week
     * @return
     */
    public static Date getFirstDayOfWeek(int year, int week) {
        Calendar c = new GregorianCalendar();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, Calendar.JANUARY);
        c.set(Calendar.DATE, 1);

        Calendar cal = (GregorianCalendar) c.clone();
        cal.add(Calendar.DATE, week * 7);

        return getFirstDayOfWeek(cal.getTime());
    }

    /**
     * 获取某年的第几周的结束日期
     *
     * @param year
     * @param week
     * @return
     */
    public static Date getLastDayOfWeek(int year, int week) {
        Calendar c = new GregorianCalendar();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, Calendar.JANUARY);
        c.set(Calendar.DATE, 1);

        Calendar cal = (GregorianCalendar) c.clone();
        cal.add(Calendar.DATE, week * 7);

        return getLastDayOfWeek(cal.getTime());
    }

    /**
     * 获取当前时间所在周的开始日期
     *
     * @param date
     * @return
     */
    public static Date getFirstDayOfWeek(Date date) {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setTime(date);
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday
        return c.getTime();
    }

    /**
     * 获取当前时间所在周的结束日期
     *
     * @param date
     * @return
     */
    public static Date getLastDayOfWeek(Date date) {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setTime(date);
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday
        return c.getTime();
    }

    /**
     * 年
     *
     * @return
     */
    public static int geryear() {
        Calendar rightNow = Calendar.getInstance();
        return rightNow.getWeekYear();
    }

    /**
     * 获得当前时间
     */
    public static Date currentTime() {
        return new Date();
    }

    /**
     * 根据时间返回格式化的字符串
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String format(Date date, String pattern) {
        if (date == null) {
            return null;
        }

        return new SimpleDateFormat(pattern).format(date.getTime());
    }
}
